﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using RevitInteractiveUtility.ExternalEventManager;

namespace FetchDataCompare
{
	public class DualLinkageRequest : IRequest
	{
		public int RequestId
		{
			get
			{
				return 0;
			}
		}

		public void DoRequest(object parameters = null)
		{
			DualLinkageBean dualLinkageBean = null;
			if (parameters is DualLinkageBean)
			{
				dualLinkageBean = (parameters as DualLinkageBean);
			}
			this.DoDualLinkage(dualLinkageBean.UiDoc, dualLinkageBean.FirstView, dualLinkageBean.FirstUIView, dualLinkageBean.SecondView, dualLinkageBean.SecondUIView);
		}

		private void DoDualLinkage(UIDocument uiDoc, View FirstView, UIView FirstUIView, View SecondView, UIView SecondUIView)
		{
			try
			{
                if ((int)FirstView.ViewType == 4 && (int)SecondView.ViewType == 4)
				{
					Transaction transaction = new Transaction(uiDoc.Document, "View3D Synchro");
					transaction.Start();
					View3D view3D = FirstView as View3D;
					View3D view3D2 = SecondView as View3D;
					if (uiDoc.ActiveView.Document.PathName == FirstView.Document.PathName)
					{
						ViewOrientation3D orientation = view3D.GetOrientation();
						view3D2.SetOrientation(orientation);
					}
					if (uiDoc.ActiveView.Document.PathName == SecondView.Document.PathName)
					{
						ViewOrientation3D orientation2 = view3D2.GetOrientation();
						view3D.SetOrientation(orientation2);
					}
					transaction.Commit();
				}
			}
			catch (Exception)
			{
			}
			try
			{
				if (uiDoc.ActiveView.Document.PathName == FirstView.Document.PathName)
				{
					IList<XYZ> zoomCorners = FirstUIView.GetZoomCorners();
					SecondUIView.ZoomAndCenterRectangle(zoomCorners.First<XYZ>(), zoomCorners.Last<XYZ>());
				}
				if (uiDoc.ActiveView.Document.PathName == SecondView.Document.PathName)
				{
					IList<XYZ> zoomCorners2 = SecondUIView.GetZoomCorners();
					FirstUIView.ZoomAndCenterRectangle(zoomCorners2.First<XYZ>(), zoomCorners2.Last<XYZ>());
				}
			}
			catch (Exception)
			{
			}
		}
	}
}
